package com.example.test.advice.第六章_枚举和注解.建议87_使用valueOf前必须进行校验;

import java.util.Arrays;
import java.util.List;

public class Client {

    public static void main(String[] args) {
        test();
        for(Season season:Season.values()){
            System.out.println("name:"+season.name());
        }
    }

    private static void test(){
        //注意summer是小写
        List<String> params = Arrays.asList("Spring","summer");
        for(String name:params){
            //查找字面值与name相同的枚举项
            Season s = Season.valueOf(name);
            if(s != null){
                //有该枚举项时
                System.out.println(s);
            }else {
                //无相关枚举项时
                System.out.println("无相关枚举项");
            }
        }
    }

    //改进方式
    private static void test2(){
        //注意summer是小写
        List<String> params = Arrays.asList("Spring","summer");
        for(String name:params){
            if(Season.contains(name)){
                Season s = Season.valueOf(name);
                //有该枚举项时
                System.out.println(s);
            }else {
                //无相关枚举项时
                System.out.println("无相关枚举项");
            }
        }
    }
}
